
/****
 * Copyright (C) 2006 @autors                                            
 *                                                            ****
 * This program is free software; you can redistribute it and/or *
 * modify under the terms of the GNU General Public License as   *
 * published by the Free Software Foundation;                    *
 * either version 2 of the License.                              *
 *                                                               *
 * This program is distributed in the hope that it will be       *
 * useful, but WITHOUT ANY WARRANTY; without even the implied    *
 * warranty of without even the implied waranty of               *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.          *
 * See the GNU General Public License for more details.          *
 *                                                               *
 * You should have received a copy of the                        *
 * GNU General Public License                                    *
 * along with this program; if not, write to the                 *
 * Free Software Foundation, Inc.,                               *
 * 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. *
 *                                                               *
 ****                                                         ****/

#include <dcore.h>
#include <dwget.h>

struct {
  char *dest;
  unsigned size;
  char *sha1;
  char *sha256;
  char *sha512;
  char *md5;
} options;

static d_option_t option[] = {
  {"--dest",        "-d",      "file destination", D_OPTION_STRING, &options.dest },
  {"--size",        "-s",      "file size",        D_OPTION_INTEGER, &options.size },
  {"--hash-sha1",   "-sha1",   "hash sha1",        D_OPTION_STRING, &options.sha1 },
  {"--hash-sha256", "-sha256", "hash sha256",      D_OPTION_STRING, &options.sha256 },
  {"--hash-sha512", "-sha512", "hash sha512",      D_OPTION_STRING, &options.sha512 },
  {"--hash-md5",    "-md5",    "hash md5",         D_OPTION_STRING, &options.md5 },
  {NULL},
};

void
init_options()
{
  memset(&options, 0, sizeof(options));
}

int
main (int argc, char **argv)
{
  d_error_t *error=NULL;
  d_wget_t *wget=NULL;

  init_options();
  d_hash_init();

  if (argc == 1) {
    fprintf(stderr, "%s --help\n", argv[0]);
    return EXIT_FAILURE;
  }

  d_option_parse(option, &argc, &argv, &error);
  if (error) {
    d_error_display(error);
    d_error_free(&error);
    return EXIT_FAILURE;
  }

  if (argc < 2) {
    fprintf(stderr, "url is needed.\n");
    return EXIT_FAILURE;
  }

  if (options.dest == NULL) {
    options.dest = strrchr(argv[1], '/');
    if (options.dest == NULL) {
      fprintf(stderr, "url is not valid.");
      return EXIT_FAILURE;
    }
    options.dest++;
  }

  wget = d_wget_new();
  d_wget_set_url(wget, argv[1]);
  d_wget_set_dest(wget, options.dest);
  d_wget_set_user_agent(wget, "dwget");
  
  if (options.md5 != NULL) {
    d_wget_set_hash(wget, "md5", options.md5);
  }
  if (options.sha1 != NULL) {
    d_wget_set_hash(wget, "sha1", options.sha1);
  }
  if (options.sha256 != NULL) {
    d_wget_set_hash(wget,"sha256", options.sha256);
  }
  if (options.sha512 != NULL) {
    d_wget_set_hash(wget, "sha512", options.sha512);
  }
  if (options.size != 0) {
    d_wget_set_size(wget, options.size);
  }

  d_wget_perform(wget, &error);
  if (error) {
    d_error_display(error);
    d_error_free(&error);
  }

  d_wget_result_display(wget);
  d_wget_free(&wget);

  return EXIT_SUCCESS;
}
